package com.example;

import com.wholefarm.model.SpecailApplicationFactory;
import com.wholefarm.model.SpecailApplicationInterface;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.web.servlet.ServletComponentScan;
import org.springframework.context.annotation.Bean;
import public_util.pack_4_stable.servlet.PublicInitMgrServlet;
import system_api_4_trade.DuzikaiSpecialApplicationFactory;
import system_api_4_trade.SpecialApplicationAfter;
import system_api_4_trade.SpecialApplicationBefore;

import javax.servlet.ServletException;

@SpringBootApplication(scanBasePackages={"com.**","public_util.**","system_api_4_trade.**","tutorial.**"})
@ServletComponentScan(basePackages = {"com.**","public_util.**","system_api_4_trade.**","tutorial.**"})
public class ExampleApplication {

    @Value("${spring.profiles.active}")
    private String config_environment;//local 本地, pro生产, test远程测试

    public static void main(String[] args) {
        SpringApplication.run(ExampleApplication.class, args);
    }

    @Bean(
            name = {"publicInitMgrServlet"}
    )
    public PublicInitMgrServlet publicInitMgrServlet() throws ServletException {
        initNewSepcailApplication();//新版本接口1
        PublicInitMgrServlet publicInitMgrServlet = new PublicInitMgrServlet();
        publicInitMgrServlet.init();
        return publicInitMgrServlet;
    }



    /**
     * 新的application操作
     */
    private void initNewSepcailApplication()
    {
        //数据库的初始化需要, 需要完成, init方法里面, 还没有处理好

        /*按照独自开重构思路, 进行相关的特殊处理的方法*/
        SpecialApplicationBefore beforeApp = new SpecialApplicationBefore4Example();

        //local 本地, pro生产, test远程测试
        if ("local".equals(config_environment))
        {
            //本地环境
            beforeApp.setConfiguartion_environment(new String[]{SpecailApplicationInterface.LOAD_CONF_TYPE_FROM_DB, "1021"});
        }
        if ("test".equals(config_environment))
        {
            //测试环境
            beforeApp.setConfiguartion_environment(new String[]{SpecailApplicationInterface.LOAD_CONF_TYPE_FROM_DB, "1022"});
        }
        if ("pro".equals(config_environment))
        {
            //正式环境
            beforeApp.setConfiguartion_environment(new String[]{SpecailApplicationInterface.LOAD_CONF_TYPE_FROM_DB, "102"});
        }
        SpecialApplicationAfter afterApp = new SpecialApplicationAfter4Example();
        DuzikaiSpecialApplicationFactory.setInitApplication(beforeApp, afterApp);
    }
}
